﻿/**
 *	(C) 2014 Yuanjj
 *
 *	define ui protocal thread class
 *
 *	histroy
 * 		2014-08-07	yuanjj	- create the file
 */

#include "ui_thread.h"
#include "errorcode.h"
#include "routine/global.h"
#include "routine/protocal/ui_protocal.h"

ui_thread::ui_thread()
{
    m_tunnel	= CL_UI;
    m_main		= MAIN_UI;
}

ui_thread::~ui_thread()
{
}

/**
 *	deal protocal recv from the ui tunnel
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::deal_protocal( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res;
    head_in&		head = *(head_in*)pdata;

    res = base_thread::deal_protocal( pdata, size );
    if ( PEM_OK != res )
        return res;

    switch ( head.second )
    {
    case UI_USER_LOGIN:
        res = _user_login( pdata, size ); break;
    case UI_HOST:
        res = _update_host( pdata, size ); break;
    case UI_TUNNEL_TYPE:
        res = _update_tunnel_type( pdata, size ); break;
    case UI_TUNNEL:
        res = _update_tunnel( pdata, size ); break;
    case UI_DEV_TYPE:
        res = _update_device_type( pdata, size ); break;
    case UI_DEVICE:
        res = _update_device( pdata, size ); break;
    case UI_PROTOCAL:
        res = _update_protocal( pdata, size ); break;
    case UI_ALARM:
        res = _update_alarm( pdata, size ); break;
    case UI_EVENT_TYPE:
        res = _update_event_type( pdata, size ); break;
    case UI_EVENT:
        res = _update_event( pdata, size ); break;
    case UI_SCENE:
        res = _update_scene( pdata, size ); break;
    case UI_SCENE_NEW:
        res = _new_scene( pdata, size ); break;
    case UI_FRAME:
        res = _update_frame( pdata, size ); break;
    default:
        res = BUILD_ERRORCODE( 0, PEM_PROTOCAL_UNKNOW );
        g_log.write( LOG_WARN, "ui protocal", "Unknow protocal second(%d)", head.second );
        break;
    }

    return	res;
}

/**
 *	user login
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_user_login( pem_s8_t* pdata, pem_s32_t size )
{
    pem_error_t			res 		= PEM_OK;
    ui_user_login		*uilogin	= (ui_user_login*)(pdata + sizeof(head_in));
    QString				js_fun;

    js_fun.sprintf( "callback_user_login( \"%s\", %d, %d );", uilogin->account, uilogin->privilege, uilogin->status );

    emit call_js( js_fun );

    return res;
}

/**********************************************************
 *	host data
 **********************************************************/
/**
 *	update host
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_host( pem_s8_t* pdata, pem_s32_t size )
{
    pem_error_t			res 		= PEM_OK;
    pem_u32_t			bodysize	= 0;
    head_in&			head 		= *(head_in*)pdata;
    ui_host				*host		= (ui_host*)(&head + 1);
    QString				js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_host) )
    {
        js_fun.sprintf( "callback_set_host( %d, \"%s\", \"%s\", \"%s\", %u, %d );",
                            host->serial, host->host, host->name, host->ip, host->port, host->status );

        emit call_js( js_fun );
        host++;
    }

    return res;
}

/**
 *	update tunnel type to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_tunnel_type( pem_s8_t* pdata, pem_s32_t size )
{
    pem_error_t			res			= PEM_OK;
    pem_u32_t			bodysize	= 0;
    head_in&			head 		= *(head_in*)pdata;
    ui_tunnel_type*		ptype		= (ui_tunnel_type*)(&head + 1);
    QString				js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_tunnel_type) )
    {
        js_fun.sprintf( "callback_set_tunnel_type( %d, \"%s\" );", ptype->serial, ptype->type );
        emit call_js( js_fun );
        ptype++;
    }

    return res;
}

/**
 *	update tunnel to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_tunnel( pem_s8_t* pdata, pem_s32_t size )
{
    pem_error_t			res			= PEM_OK;
    pem_u32_t			bodysize	= 0;
    head_in&			head 		= *(head_in*)pdata;
    ui_tunnel*			ptunnel		= (ui_tunnel*)(&head + 1);
    QString				js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_tunnel) )
    {
        js_fun.sprintf( "callback_set_tunnel( %d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", %d );",
                        ptunnel->serial, ptunnel->name, ptunnel->type, ptunnel->number,
                        ptunnel->host, ptunnel->remark, ptunnel->used );
        emit call_js( js_fun );
        ptunnel++;
    }

    return res;
}

/**
 *	update device type to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_device_type( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    pem_u32_t		bodysize	= 0;
    head_in&		head 		= *(head_in*)pdata;
    ui_dev_type*	ptype		= (ui_dev_type*)(&head + 1);
    QString			js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_dev_type) )
    {
        js_fun.sprintf( "callback_set_device_type( %d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\" );",
                        ptype->serial, ptype->type, ptype->name, ptype->probe, ptype->img, ptype->append );
        emit call_js( js_fun );
        ptype++;
    }

    return res;
}

/**
 *	update device to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_device( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    pem_u32_t		bodysize	= 0;
    head_in&		head 		= *(head_in*)pdata;
    ui_device*		pdevice		= (ui_device*)(&head + 1);
    QString			js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_device) )
    {
        js_fun.sprintf( "callback_set_device("
                        " \"%s\", \"%s\", \"%s\", \"%s\", \"%s\","
                        " \"%s\", \"%s\", \"%s\", \"%s\", \"%s\","
                        " %d, %d, %d );",
                        pdevice->dev_id, 	pdevice->dev_type, 		pdevice->dev_name, pdevice->dev_pos, 	pdevice->add_time,
                        pdevice->machine,	pdevice->tunnel_num, 	pdevice->pto_name, pdevice->host, 		pdevice->value,
                        pdevice->x, 		pdevice->y, 			pdevice->setup );
        emit call_js( js_fun );
        pdevice++;
    }

    return res;
}

/**
 *	update protocal to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_protocal( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    pem_u32_t		bodysize	= 0;
    head_in&		head 		= *(head_in*)pdata;
    ui_protocal*	protocal	= (ui_protocal*)(&head + 1);
    QString			js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_protocal) )
    {
        js_fun.sprintf( "callback_set_protocal( %d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", %d );",
                        protocal->serial, protocal->pto_name, protocal->pto_file, protocal->tunnel_type,
                        protocal->dev_type, protocal->remark, protocal->used );
        emit call_js( js_fun );
        protocal++;
    }

    return res;
}

/**
 *	update alarm to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_alarm( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    pem_u32_t		bodysize	= 0;
    head_in&		head 		= *(head_in*)pdata;
    ui_alarm*		alarm		= (ui_alarm*)(&head + 1);
    QString			js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_alarm) )
    {
        js_fun.sprintf( "callback_set_alarm( %u, \"%s\", \"%s\", \"%s\", %d, \"%s\", %d, \"%s\", \"%s\" );",
                        alarm->serial, alarm->host, alarm->dev_id, alarm->cmp_start, alarm->alm_start,
                        alarm->cmp_stop, alarm->alm_stop, alarm->link_host, alarm->link_dev );
        emit call_js( js_fun );
        alarm++;
    }

    return res;
}

/**
 *	update event type to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_event_type( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t			res				= PEM_OK;
    pem_u32_t			bodysize		= 0;
    head_in&			head 			= *(head_in*)pdata;
    ui_event_type*		event_type		= (ui_event_type*)(&head + 1);
    QString				js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_event_type) )
    {
        js_fun.sprintf( "callback_set_event_type( %u, \"%s\" );", event_type->serial, event_type->type );
        emit call_js( js_fun );
        event_type++;
    }

    return res;
}

/**
 *	update event to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_event( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    pem_u32_t		bodysize	= 0;
    head_in&		head 		= *(head_in*)pdata;
    ui_event*		event		= (ui_event*)(&head + 1);
    QString			js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_event) )
    {
        js_fun.sprintf( "callback_set_event( %u, \"%s\", \"%s\", \"%s\", \"%s\" );",
                        event->serial, event->type, event->datetime, event->user, event->descript );
        emit call_js( js_fun );
        event++;
    }

    return res;
}

/**
 *	update scene to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_scene( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    head_in&		head 		= *(head_in*)pdata;
    ui_scene*		scene		= (ui_scene*)(&head + 1);
    QString			js_fun;

    js_fun.sprintf( "callback_set_scene( \"%s\" );", scene->img );
    emit call_js( js_fun );

    return res;
}

/**
 *	set new scene to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_new_scene( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    head_in&		head 		= *(head_in*)pdata;
    ui_scene_new*	scene		= (ui_scene_new*)(&head + 1);
    QString			js_fun;

    js_fun.sprintf( "callback_new_scene( \"%s\" );", scene->img );
    emit call_js( js_fun );

    return res;
}

/**
 *	update frame to ui
 *
 * 	@pdata			[in] the protocal buffer
 * 	@size			[in] the buffer size
 *
 * 	return
 * 		==	PEM_OK		protocal is good
 * 		!=	PEM_OK		errorcode
 */
pem_error_t ui_thread::_update_frame( pem_s8_t* pdata, pem_u32_t size )
{
    pem_error_t		res			= PEM_OK;
    pem_u32_t		bodysize	= 0;
    head_in&		head 		= *(head_in*)pdata;
    ui_frame*		frame		= (ui_frame*)(&head + 1);
    QString			js_fun;

    for ( bodysize = 0; bodysize < head.size; bodysize += sizeof(ui_frame) )
    {
        js_fun.sprintf( "callback_create_main( );" );
        emit call_js( js_fun );
        frame++;
    }

    return res;
}
